#include <func.h>

void* start_routine(void* arg) {
	// 改变取消状态
	// int err;
	// int oldstate;
	// err = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
	// err = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
	
	// int oldtype;
	// err = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
	// err = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
	// if (err) {
		// error(1, err, "pthread_setcanceltype");
	// }

	for(;;) {
		// sleep(1); // 取消点
		// 手动设置取消点
		pthread_testcancel();
	}
	return (void*)9527;
}

int main(int argc, char* argv[])
{
	int err;
	pthread_t tid;
	err = pthread_create(&tid, NULL, start_routine, NULL);
	if (err) {
		error(1, err, "pthread_create");
	}


	sleep(1);

	// 发送取消请求
	err = pthread_cancel(tid);
	if (err) {
		error(1, err, "pthread_cancel");
	}

	// 连接新线程
	void* retval;
	err = pthread_join(tid, &retval);
	if (err) {
		error(1, err, "pthread_join");
	}

	if (retval == PTHREAD_CANCELED) {
		printf("thread 0x%lx has been cancelled\n", tid);
	}
    return 0;
}

